
import requests
from lxml import etree
import os
if __name__ == '__main__':
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                      ' Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.76'
    }
    url = 'https://pic.netbian.top/4kfengjing/'
    page_text = requests.get(url = url,headers = headers)
    #手动设定响应数据的编码格式
    # page_text.encoding = 'utf-8'
    # page_text.encoding = 'gbk'

    #创建一个文件夹
    if not os.path.exists('./pic'):
        os.mkdir('./pic')

    # tree = etree.HTML(page_text)
    # li_list = tree.xpan('//div[@class = "slist"]/ul/li')
    # for li in li_list:
    #     img_src = 'http://www.xxxx.com'+li.xpath('.//span/div/@src')
    #     img_name = li.xpath('./a/img/@alt')[0] + '.jpg'
    #
    #     #img_name出现乱码
    #     img_name = img_name.encode('iso-8859-1').decode('gbk')
    #     print(img_name,img_src)
    #
    #     img_data = requests.get(url = img_src,headers=headers).content
    #     img_path = 'pic'+img_name
    #     with open(img_path,'wb')as fp:
    #         fp.write(img_data)
    #         print('成功')






    # 该网页对图片的url进行内部隐藏，未直接使用src='www.xx.com'形式。故无法使用xpath路径进行爬取。可以尝试使用正则表达式方法
    #background-image: url(https://img1.huamaocdn.com/upload/bizhi/images/280h/2020/02/202106101937555386.jpg);




